package cn.gson.boot.controller;

import cn.gson.boot.model.pojos.*;
import cn.gson.boot.model.service.HeTongService;
import cn.gson.boot.model.service.JiaoFuJiHuaService;
import cn.gson.boot.model.service.JiaoFuJiLuLuLuService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/jfjilu")
public class JiaoFuJiLuLuLuController {
    @Autowired
    HeTongService heTongService;
    @Autowired
    JiaoFuJiHuaService jiaoFuJiHuaService;

    @Autowired
    JiaoFuJiLuLuLuService jiaoFuJiLuLuLuService;

    //查看所有订单
    @RequestMapping("/jlselectAllDingDan")
    @ResponseBody
    public List<Dingdan> jlselectAllDingDan(){
        return heTongService.selectAllDingDan(new Dingdan(null));
    }

    /*//根据选择的订单（订单号）找到此订单所有的交付计划
    @RequestMapping("/selectJJByddHao")
    @ResponseBody
    public List<Jiaofujihua> selectJJByddHao(String ddHao){
        List<Jiaofujihua> jj = jiaoFuJiHuaService.selectAllJiaoFuJiHua(ddHao);
        List<Jiaofujilu> jl = jiaoFuJiLuLuLuService.selectAllJiaoFuJiLuLuLu(null);
        System.out.println("22:"+jl.get(0).getJiaofujihuaByJjId().getJjHao());
        //如果交计已经存在交录中，就在jj中移除
        for(int i=0;i<jj.size();i++){
            for(int j=0;j<jl.size();j++){
                if(jj.get(i).getJjHao().equals(jl.get(j).getJiaofujihuaByJjId().getJjHao())){
                    System.out.println("1:"+jj.get(i).getJjHao());
                    System.out.println("2:"+jl.get(j).getJiaofujihuaByJjId().getJjHao());
                    jj.remove(i);
                    break;
                }
            }
        }
        return jj;

    }*/

    //查询所有交付记录
    @RequestMapping("/selectAllJiaoFuJiLuLuLu")
    @ResponseBody
    public Map<String,Object> selectAllJiaoFuJiLuLuLu(String ddHao,String khName,String time, Integer pageNumm, Integer pageSizee){
        Page<Object> objects = PageHelper.startPage(pageNumm, pageSizee);

        String time3 = null;
        if(time != null && time != "" && !time.equals("")){
            StringBuffer time1 = new StringBuffer(time.substring(0,10));
            System.err.println("tim1:"+time1);
            //日期这里会有问题，如果是月底的话就会出错，要判断一下是大月还是小月，是否是闰年
            int ti = Integer.parseInt(time1.substring(8))+1;
            System.err.println("ti:"+ti);
            String tistr = toString().valueOf(ti);
            if(toString().valueOf(ti).length() == 1){
                tistr = 0 + toString().valueOf(ti);
            }
            StringBuffer time2 = new StringBuffer(time1.replace(8,time1.length(), tistr));
            System.err.println("tim2:"+time2);
            time3 = time2.toString();
        }
        List<Jiaofujilu> list = jiaoFuJiLuLuLuService.selectAllJiaoFuJiLuLuLu(ddHao,khName,time3);

        Map<String,Object> map = new HashMap();
        map.put("total", objects.getTotal());
        map.put("list", list);
        return map;
    }


    //根据交计id找到交计详情
    @RequestMapping("/selectJJXQbyjjId")
    @ResponseBody
    public List<Jiaofujihuaxiangqing> selectJJXQbyjjId(Integer jjId) {
        return jiaoFuJiHuaService.ckJJXQbyjjId(jjId);
    }

    /*//新增交付记录
    @RequestMapping("/addJiaoFuJiLu")
    @ResponseBody
    public void addJiaoFuJiLu(@RequestBody Jiaofujilu jiaofujilu) {
       jiaoFuJiLuLuLuService.addJiaoFuJiLu(jiaofujilu);
    }
*/
    //根据记录id查找交录详情
    @RequestMapping("/selectJLXQbyjjId")
    @ResponseBody
    public List<Jiaofujluxiangqing> selectJLXQbyjjId(Integer jlId){
        return jiaoFuJiLuLuLuService.selectJLXQbyjjId(jlId);
    }

    //移除交付记录
    @RequestMapping("/yichuJiaoFuJiLu")
    @ResponseBody
    public void yichuJiaoFuJiLu(Integer jlId){
        jiaoFuJiLuLuLuService.yichuJiaoFuJiLu(jlId);
    }
}
